package com.kavin.checknetwork;

import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kavin.network.NetworkUtils;

public class NetworkActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);

        NetworkUtils.setOnChangeListener(this, new NetworkUtils.OnChangeListener() {
            @Override
            public void onChange(NetworkInfo networkInfo, boolean isConnected) {
                if (networkInfo == null) {
                    Log.d("------NetworkActivity", "网络无连接" + " isConnected: " + isConnected);
                    Toast.makeText(NetworkActivity.this, "网络无连接", Toast.LENGTH_SHORT).show();
                } else {
                    String typeName = networkInfo.getTypeName();
                    Log.d("------NetworkActivity", typeName + " isConnected: " + isConnected);
                    Toast.makeText(NetworkActivity.this, typeName + " isConnected: " + isConnected, Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    public void getConnectionType(View view) {
        int connectionType = NetworkUtils.getConnectionType(this);
        String typeStr = "unknow";
        switch (connectionType) {
            case NetworkUtils.CMNET:
                typeStr = "CMNET";
                break;
            case NetworkUtils.CMWAP:
                typeStr = "CMWAP";
                break;
            case NetworkUtils.WIFI:
                typeStr = "WIFI";
                break;
        }
        Toast.makeText(this, typeStr, Toast.LENGTH_SHORT).show();
    }

    public void getTypeName(View view) {
        String typeName = NetworkUtils.getTypeName(this);
        Toast.makeText(this, typeName, Toast.LENGTH_SHORT).show();
    }

    public void getPhoneIp(View view) {
        String phoneIp = NetworkUtils.getPhoneIp();
        Toast.makeText(this, phoneIp, Toast.LENGTH_SHORT).show();
    }

    public void getWifiMacAddress(View view) {
        String wifiMacAddress = NetworkUtils.getWifiMacAddress(this);
        Toast.makeText(this, "wifiMacAddress：" + wifiMacAddress, Toast.LENGTH_SHORT).show();
    }

    public void isNetworkAvailable(View view) {
        boolean isNetworkAvailable = NetworkUtils.isNetworkAvailable(this);
        Toast.makeText(this, "isNetworkAvailable：" + isNetworkAvailable, Toast.LENGTH_SHORT).show();
    }

    public void isNetworkFor2G(View view) {
        boolean isNetworkFor2G = NetworkUtils.isNetworkFor2G(this);
        Toast.makeText(this, "isNetworkFor2G：" + isNetworkFor2G, Toast.LENGTH_SHORT).show();
    }

    public void isWifi(View view) {
        boolean isWifi = NetworkUtils.isWifi(this);
        Toast.makeText(this, "isWifi：" + isWifi, Toast.LENGTH_SHORT).show();
    }


}
